package com.guosheng.test20181129;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.List;

public class BeanToXml {

    public static String beanToXml(Object obj,Class<?> load) throws JAXBException {
        JAXBContext context = JAXBContext.newInstance(load);
        Marshaller marshaller = context.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,true);
        marshaller.setProperty(Marshaller.JAXB_ENCODING,"utf-8");
        StringWriter writer = new StringWriter();
        marshaller.marshal(obj,writer);
        return writer.toString();
    }

    public static void main(String[] args) throws JAXBException {
       /* List<String> hobby = new ArrayList<>();
        hobby.add("吃");
        hobby.add("喝");
        hobby.add("玩");
        hobby.add("乐");
        List<Student> list = new ArrayList<>();
        Student s1 = new Student("zhangsan","nan",80,"sanban",hobby);
        Student s2 = new Student("lisi","nan",80,"sanban",hobby);
        Student s3 = new Student("wangwu","nan",80,"sanban",hobby);
        Student s4 = new Student("zhaoliu","nan",80,"sanban",hobby);
        list.add(s1);
        list.add(s2);
        list.add(s3);
        list.add(s4);
        Grade grade = new Grade("计算机",99);
        StudentList st = new StudentList();
        st.setStudents(list);
        st.setGrade(grade);
        String str = BeanToXml.beanToXml(st,StudentList.class);
        System.out.println(str);*/

       StudentList studentList = new StudentList();
       String xml = StudentList.userconvertToXml(studentList,StudentList.class);
       System.out.println(xml);
    }
}
